/**
 * Copyright (C) 2017 PatSnap Pte Ltd, All Rights Reserved.
 */
package com.job.service;

import com.job.bean.Comment;
import com.job.interfaces.CommentThriftDto;
import com.job.interfaces.CommentThriftService;

import org.apache.thrift.TException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: Tory
 * Date: 4/16/17
 * Time: 7:16 PM
 */
@Service
public class CommentThriftServiceImpl implements CommentThriftService.Iface {

    @Autowired
    private CommentService commentService;

    @Override
    public List<CommentThriftDto> getCommentThrift(int goodId) throws TException {
        List<Comment> commentList = commentService.findCommentByGoodId(goodId);
        ArrayList<CommentThriftDto> commentThriftDtoList = new ArrayList<>();
        commentList.forEach(comment -> {
            CommentThriftDto commentThriftDto = new CommentThriftDto();
            commentThriftDto.setGoodId(comment.getGoodId());
            commentThriftDto.setName(comment.getName());
            commentThriftDto.setId(comment.getId());
            commentThriftDtoList.add(commentThriftDto);
        });
        return commentThriftDtoList;
    }

    @Override
    public void saveCommentThrift(CommentThriftDto commentThriftDto) throws TException {
        Comment comment = new Comment();
        comment.setName(commentThriftDto.getName());
        comment.setGoodId(commentThriftDto.getGoodId());
        commentService.save(comment);
    }

    @Override
    public void deleteCommentThrift(int id) throws TException {
        commentService.delete(id);
    }
}
